﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BTGISWASECO.DataLayer;
using BTGISWASECO.BusinessEntities;
using BTGISWASECO.BusinessLayer;

public partial class PresentationLayer_DefaultTab : System.Web.UI.Page
{
    MayBomBLL bll = new MayBomBLL();
    DMHuongDongChayDAL DMHuongDongChayDal = new DMHuongDongChayDAL();
    MayBomEntities MayBomEntities = new MayBomEntities();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["DataTable"] == "")
                Response.Redirect("../default.aspx");
            MayBomEntities = bll.GetMayBomEntitiesByID();
            txtObjectID.Value = MayBomEntities.OBJECTID.ToString();

            this.txtIDMayBom.Text = MayBomEntities.IDMayBom;
            this.dLuuLuongTK.Text = MayBomEntities.LuuLuongTK.ToString();
            this.dLuuLuongCD.Text = MayBomEntities.LuuLuongCD.ToString(); 
            this.dApLucTK.Text = MayBomEntities.ApLucTK.ToString();
            this.dApLucCD.Text = MayBomEntities.ApLucCD.ToString();

            foreach (var itemtc in DMHieuMayBomDAL.GetList())
            {
                if (MayBomEntities.NhanHieuMBText.ToString().Trim() == itemtc.MoTa.ToString().Trim())
                    MayBomEntities.NhanHieuMB = itemtc.Ma;
                ListItem iteml = new ListItem(itemtc.MoTa.ToString(), itemtc.Ma.ToString());
                DListNhanHieuMB.Items.Add(iteml);
            }
            DListNhanHieuMB.SelectedValue = MayBomEntities.NhanHieuMB.ToString();

            foreach (var itemnsx in DMNhaSanXuatDAL.GetList())
            {
                if (MayBomEntities.NhaSanXuatText.ToString().Trim() == itemnsx.MoTa.ToString().Trim())
                    MayBomEntities.NhaSanXuat = itemnsx.Ma;
                ListItem iteml = new ListItem(itemnsx.MoTa.ToString(), itemnsx.Ma.ToString());
                DListNhaSX.Items.Add(iteml);
            }
            DListNhaSX.SelectedValue = MayBomEntities.NhaSanXuat.ToString();

            foreach (var itemnusx in DMNuocSanXuatDAL.GetList())
            {
                if (MayBomEntities.NuocSanXuatText.ToString().Trim() == itemnusx.MoTa.ToString().Trim())
                    MayBomEntities.NuocSanXuat = itemnusx.Ma;
                ListItem iteml = new ListItem(itemnusx.MoTa.ToString(), itemnusx.Ma.ToString());//
                DListNuocSX.Items.Add(iteml);
            }
            DListNuocSX.SelectedValue = MayBomEntities.NuocSanXuat.ToString();

            this.txtNgayLapDat.Text = MayBomEntities.NgayLDMayBom.ToString("dd/MM/yyyy");

            foreach (var itemkcv in DMLoaiMayBomDAL.GetList())
            {
                if (MayBomEntities.KieuMayBomText != null)
                    if (MayBomEntities.KieuMayBom.ToString().Trim() == itemkcv.MoTa.ToString().Trim())
                        MayBomEntities.KieuMayBom = itemkcv.Ma;
                ListItem iteml = new ListItem(itemkcv.MoTa.ToString(), itemkcv.Ma.ToString());
                DListKieuMayBom.Items.Add(iteml);
            }
            if (MayBomEntities.KieuMayBom != null)
            DListKieuMayBom.SelectedValue = MayBomEntities.KieuMayBom.ToString();

            this.txtChucNang.Text = MayBomEntities.ChucNang.ToString();

            this.dDuongKinhOH.Text = MayBomEntities.DuongKinhOH.ToString();
            this.dDuongKinhOD.Text = MayBomEntities.DuongKinhOD.ToString();
            this.dRPM.Text = MayBomEntities.RPM.ToString();
            this.dCongSuatDC.Text = MayBomEntities.CongSuatDC.ToString();
            this.dCuongDoDC.Text = MayBomEntities.CuongDotDC.ToString();
            this.dHieuSuatBom.Text = MayBomEntities.HieuSuat.ToString();
            this.dGocLechPha.Text = MayBomEntities.GocLechPha.ToString();
            foreach (var itemnttsd in DMTinhTrangSDDAL.GetList())
            {
                if (MayBomEntities.TinhTrangText.ToString().Trim() == itemnttsd.MoTa.ToString().Trim())
                    MayBomEntities.TinhTrang = itemnttsd.Ma;
                ListItem iteml = new ListItem(itemnttsd.MoTa.ToString(), itemnttsd.Ma.ToString());
                DListTTSD.Items.Add(iteml);
            }
            DListTTSD.SelectedValue = MayBomEntities.TinhTrang.ToString();

            this.txtNgayLDBienTan.Text = MayBomEntities.NgayLDBienTan.ToString("dd/MM/yyyy");

            foreach (var itemvl in DMNhaSanXuatDAL.GetList())
            {
                if (MayBomEntities.NhaSxBienTanText.ToString().Trim() == itemvl.MoTa.ToString().Trim())
                    MayBomEntities.NhaSxBienTan = itemvl.Ma;
                ListItem iteml = new ListItem(itemvl.MoTa.ToString(), itemvl.Ma.ToString());
                DListNhaSXBienTan.Items.Add(iteml);
            }
            DListNhaSXBienTan.SelectedValue = MayBomEntities.NhaSxBienTan.ToString();

            foreach (var itemvl in DMNuocSanXuatDAL.GetList())
            {
                if (MayBomEntities.NuocSxBienTanText.ToString().Trim() == itemvl.MoTa.ToString().Trim())
                    MayBomEntities.NuocSxBienTan = itemvl.Ma;
                ListItem iteml = new ListItem(itemvl.MoTa.ToString(), itemvl.Ma.ToString());
                DListTTSDBTan.Items.Add(iteml);
            }
            DListTTSDBTan.SelectedValue = MayBomEntities.NuocSxBienTan.ToString();

            foreach (var itemntldv in DMTinhTrangSDDAL.GetList())
            {
                if (MayBomEntities.TinhTrangBTText != null)
                    if (MayBomEntities.TinhTrangBTText.ToString().Trim() == itemntldv.MoTa.ToString().Trim())
                        MayBomEntities.TinhTrangBT = itemntldv.Ma;
                ListItem iteml = new ListItem(itemntldv.MoTa.ToString(), itemntldv.Ma.ToString());
                DListTTSDBTan.Items.Add(iteml);
            }
            DListTTSDBTan.SelectedValue = MayBomEntities.TinhTrangBT.ToString();

            foreach (var itemntldv in DMLoaiDonViDAL.GetList())
            {
                if (MayBomEntities.DonViQLText.ToString().Trim() == itemntldv.MoTa.ToString().Trim())
                    MayBomEntities.DonViQL = itemntldv.Ma;
                ListItem iteml = new ListItem(itemntldv.MoTa.ToString(), itemntldv.Ma.ToString());
                DListDonViQL.Items.Add(iteml);
            }
            DListDonViQL.SelectedValue = MayBomEntities.DonViQL.ToString();

            foreach (var itemtsh in DMThuocSoHuuDAL.GetList()) 
            {
                if (MayBomEntities.ThuocSoHuuText.ToString().Trim() == itemtsh.MoTa.ToString().Trim())
                    MayBomEntities.ThuocSoHuu = itemtsh.Ma;
                ListItem iteml = new ListItem(itemtsh.MoTa.ToString(), itemtsh.Ma.ToString());
                DListThuocSoHuu.Items.Add(iteml);
            }
            DListThuocSoHuu.SelectedValue = MayBomEntities.ThuocSoHuu.ToString();

            this.txtMoTaViTri.Text = MayBomEntities.MoTaViTri.ToString();
            this.txtNgayCNC.Text = MayBomEntities.NgayCNCuoi.ToString("dd/MM/yyyy");
            this.txtNguoiCNC.Text = MayBomEntities.NguoiCNCuoi.ToString();
            this.nSoLanSuCo.Text = MayBomEntities.SoLanSuCo.ToString();
            this.txtIDTramBom.Text = MayBomEntities.IDTramBom.ToString();
            this.txtIDHinhAnh.Text = MayBomEntities.IDHinhAnh;
        }//ThuocSoHuu
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        MayBomEntities bk = new MayBomEntities();
        MayBomDAL MayBomDAL = new MayBomDAL();
        //MayBomEntities MayBomEntities = new MayBomEntities(Convert.ToInt32(DListDonViTC.SelectedValue = DListDonViTC.SelectedValue!=""?DListDonViTC.SelectedValue:"0"),
        //    Convert.ToInt32(DListDonViGS.SelectedValue = DListDonViGS.SelectedValue!=""?DListDonViGS.SelectedValue:"0"), this.txtNguoiCNC.Text, DateTime.Parse(this.txtNgayCNC.Text.ToString()), this.txtGhiChu.Text, this.txtMaHinhAnh.Text);
        MayBomEntities MayBomEntities = new MayBomEntities(Convert.ToInt32(txtObjectID.Value),this.txtIDMayBom.Text,
           Convert.ToDouble(this.dLuuLuongTK.Text), Convert.ToDouble(this.dLuuLuongCD.Text), Convert.ToDouble(this.dApLucTK.Text),
            Convert.ToDouble(this.dApLucCD.Text),Convert.ToInt32(DListNhanHieuMB.SelectedValue = DListNhanHieuMB.SelectedValue != "" ? DListNhanHieuMB.SelectedValue : "0"),
            Convert.ToInt32(DListNhaSX.SelectedValue = DListNhaSX.SelectedValue != "" ? DListNhaSX.SelectedValue : "0"),
            Convert.ToInt32(DListNuocSX.SelectedValue = DListNuocSX.SelectedValue != "" ? DListNuocSX.SelectedValue : "0"),
            DateTime.Parse(Request.Form.Get("txtNgayLapDat")),DListKieuMayBom.SelectedValue = DListKieuMayBom.SelectedValue != "" ? DListKieuMayBom.SelectedValue : "",
            this.txtChucNang.Text,Convert.ToDouble(this.dDuongKinhOH.Text),Convert.ToDouble(this.dDuongKinhOD.Text),Convert.ToDouble(this.dRPM.Text),
            Convert.ToDouble(this.dCongSuatDC.Text),Convert.ToDouble(this.dCuongDoDC.Text),Convert.ToDouble(this.dHieuSuatBom.Text),Convert.ToDouble(this.dGocLechPha.Text),
            Convert.ToInt32(DListTTSD.SelectedValue = DListTTSD.SelectedValue != "" ? DListTTSD.SelectedValue : "0"),DateTime.Parse(Request.Form.Get("txtNgayLDBienTan")),
            Convert.ToInt32(DListNhaSXBienTan.SelectedValue = DListNhaSXBienTan.SelectedValue != "" ? DListNhaSXBienTan.SelectedValue : "0"),
            Convert.ToInt32(DListTTSDBTan.SelectedValue = DListTTSDBTan.SelectedValue != "" ? DListTTSDBTan.SelectedValue : "0"),
            Convert.ToInt32(DListTTSDBTan.SelectedValue = DListTTSDBTan.SelectedValue != "" ? DListTTSDBTan.SelectedValue : "0"),
            Convert.ToInt32(DListDonViQL.SelectedValue = DListDonViQL.SelectedValue != "" ? DListDonViQL.SelectedValue : "0"),
            Convert.ToInt32(DListThuocSoHuu.SelectedValue = DListThuocSoHuu.SelectedValue != "" ? DListThuocSoHuu.SelectedValue : "0"),
            this.txtMoTaViTri.Text,DateTime.Parse(Request.Form.Get("txtNgayCNC")),this.txtNguoiCNC.Text,Convert.ToInt32(this.nSoLanSuCo.Text),
            this.txtIDTramBom.Text,this.txtIDHinhAnh.Text);
        MayBomDAL.Update1(MayBomEntities);
        //MayBomDAL.Update(Convert.ToInt32(txtObjectID.Value), txtMaDoanOng.Text, txtDMA.Text, Convert.ToInt32(DListHDChay.SelectedValue), Convert.ToDouble(this.nChieuDai.Text));
   //     Response.Redirect("../default.aspx");
        ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "<script language='JavaScript'>alert('Cập nhật thành công'); window.location.href ='../Default.aspx';</script>");
    }
    protected void btnHuyBo_Click(object sender, EventArgs e)
    {
        Response.Redirect("../default.aspx");
    }
}